/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.product.controller;

import cn.tedu.mall.common.pojo.product.dto.AttributeAddNewDTO;
import cn.tedu.mall.common.pojo.product.dto.AttributeUpdateDTO;
import cn.tedu.mall.common.pojo.product.vo.AttributeDetailsVO;
import cn.tedu.mall.common.pojo.product.vo.AttributeStandardVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.product.constant.WebConst;
import cn.tedu.mall.product.service.IAttributeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
@Api("属性")
@RestController
@RequestMapping(value = "/pms/attributes", produces = "application/json;charset=utf-8")
public class AttributeController {

    @Autowired(required = false)
    private IAttributeService attributeService;

    @PostMapping("/addnew")
    public JsonResult<Void> addNew(@Valid AttributeAddNewDTO attributeAddnewDTO) {
        attributeService.addNew(attributeAddnewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        attributeService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, @Valid AttributeUpdateDTO attributeUpdateDTO) {
        attributeService.updateById(id, attributeUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}/details")
    public JsonResult<AttributeDetailsVO> getDetailsById(@PathVariable Long id) {
        AttributeDetailsVO attributeDetailsVO = attributeService.getDetailsById(id);
        return JsonResult.ok(attributeDetailsVO);
    }

    @GetMapping("")
    public JsonResult<JsonPage<AttributeStandardVO>> listByTemplateId(
        @RequestParam Long templateId,
        @RequestParam(required = false, defaultValue = "1") Integer page,
        @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        JsonPage<AttributeStandardVO> attributes = attributeService.listByTemplateId(templateId, page, pageSize);
        return JsonResult.ok(attributes);
    }
//    pms/attributes/sale
    @ApiOperation("CHAXUN")
    @GetMapping("/sale")
    public JsonResult<JsonPage<AttributeStandardVO>> listSaleAttributesByTemplateId(
        @RequestParam Long templateId,
        @RequestParam(required = false, defaultValue = "1") Integer page,
        @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        JsonPage<AttributeStandardVO> attributes = attributeService.listSaleAttributesByTemplateId(templateId, page, pageSize);
        return JsonResult.ok(attributes);
    }

    @GetMapping("/non-sale")
    public JsonResult<JsonPage<AttributeStandardVO>> listNonSaleAttributesByTemplateId(
        @RequestParam Long templateId,
        @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE) Integer page,
        @RequestParam(required = false, defaultValue = WebConst.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<AttributeStandardVO> attributes = attributeService.listNonSaleAttributesByTemplateId(templateId, page, pageSize);
        return JsonResult.ok(attributes);
    }

}
